<?php
namespace app\user\model;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\Field;

class FieldData extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;
    
    protected $globalScope = ['shop_id'];
    public function scopeShop_id($query)
    {
        $query->where('shop_id', ShopId());
    }
    //新增默认字段
    public static function CreateDefaultField($field_id)
    {
        //查询分类数据
        $result = Field::where('id',$field_id)->find();
        if($result['default_field']){
            foreach ($result['default_field']['name'] as $k=>$r){
                //查询该字段是否已经添加
                $data = self::where('field_id',$field_id)->where('name',$r)->find();
                if(!$data){
                    self::create([
                        'shop_id'     => ShopId(),
                        'field_id'    => $field_id,
                        'title'       => $result['default_field']['title'][$k],
                        'name'        => $result['default_field']['name'][$k],
                        'type'        => $result['default_field']['type'][$k],
                        'value'       => $result['default_field']['value'][$k],
                        'is_sys'      => $result['default_field']['is_sys'][$k],
                    ]);
                }
            }
            return "初始化完成";
        }else{
            return "没有初始化字段";
        }
    }
    //判断是否系统字段
    static function IsSysField($field_id,$value)
    {
        $result = Field::where('id',$field_id)->value('disable_field');
        $field_list = explode(",",$result);
        $data = in_array($value,$field_list);
        return $data;
    }
    //获取所有字段
    static function get_field_list($field_id) {
        $list = self::where('field_id',$field_id)->where('status','1')->order('sort_order asc,id desc')->select();
        return $list;
    }
    //获取字段代码
    static function get_field_code($field,$data) {
        // 判断是否必填
        if($field['is_required'] == '1'){
            $required = "lay-verify='required' required";
        }else{
            $required = "";
        }
        $ret = '';
        switch ($field['type']){
            //多行输入
            case "textarea":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" class="layui-textarea" {$required}>{$data[$field['name']]}</textarea>
                    EOT;
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" class="layui-textarea" {$required}>{$data['jdata'][$field['name']]}</textarea>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" class="layui-textarea" {$required}></textarea>
                    EOT;
                }
                
                break;
            //下拉选项
            case "select":
                $ret = '<option value=""></option>';
                foreach (parse_attr($field['options']) as $k=>$v) {
                    if(isset($data[$field['name']]) and $k == $data[$field['name']] and $field['is_sys'] == '1'){
                        $ret .= "<option value=\"$k\" selected>$v</option>";
                    }else if(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret .= "<option value=\"$k\" selected>$v</option>";
                    }else{
                        $ret .= "<option value=\"$k\">$v</option>";
                    }
                }
                $ret = "<select name=\"{$field['name']}\" lay-search  {$required}>" . $ret . '</select>';
                break;
            //单选按钮
            case "radio":
                $ret = '';
                foreach (parse_attr($field['options']) as $k=>$v) {
                    if(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret .= "<option value=\"$k\" selected>$v</option>";
                    }else{
                        $ret .= "<option value=\"$k\">$v</option>";
                    }
                }
                $ret = "<select name=\"{$field['name']}\" lay-search  {$required}>" . $ret . '</select>';
                break;
            //图片上传
            case "upload":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data['jdata'][$field['name']]}" autocomplete="off" placeholder="请上传{$field['title']}" class="layui-input"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-position ajax-images"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data['jdata'][$field['name']]}" autocomplete="off" placeholder="请上传{$field['title']}" class="layui-input"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-position ajax-images"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="" autocomplete="off" placeholder="请上传{$field['title']}" class="layui-input"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-position ajax-images"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                }
                
                break;
            //编辑器
            case "editor":
                if(isset($data['jdata'][$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" id="editor_{$field['name']}" style="height:300px;" {$required}>{$data[$field['name']]}</textarea>
                    EOT;
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" id="editor_{$field['name']}" style="height:300px;" {$required}>{$data['jdata'][$field['name']]}</textarea>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <textarea name="{$field['name']}" placeholder="请输入{$field['title']}" id="editor_{$field['name']}" style="height:300px;" {$required}></textarea>
                    EOT;
                }
                break;
            //多图上传
            case "photo":
                if(isset($data['photo']) and $field['is_sys'] == '1'){
                    $ret = '';
                    $ret .= <<<EOT
                        <input type="hidden" name="photo"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary ajax-photos"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                    foreach ($data['photo'] as $v) {
                        $ret .= <<<EOT
                            <input type="text" name="photo[]" value="{$v}" autocomplete="off" readonly class="layui-input">
                            <button type="button" class="layui-btn layui-btn-primary layui-btn-position delete-photo"><i class="fa fa-times-circle"></i></button>
                        EOT;
                    } 
                }else if(isset($data['jdata']['photo']) and $data['jdata']['photo'] != ''){
                    $ret = '';
                    $ret .= <<<EOT
                        <input type="hidden" name="photo"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary ajax-photos"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                    foreach ($data['jdata']['photo'] as $v) {
                        $ret .= <<<EOT
                            <input type="text" name="photo[]" value="{$v}" autocomplete="off" readonly class="layui-input">
                            <button type="button" class="layui-btn layui-btn-primary layui-btn-position delete-photo"><i class="fa fa-times-circle"></i></button>
                        EOT;
                    } 
                }else{
                    $ret = <<<EOT
                        <input type="hidden" name="photo"  {$required}>
                        <button type="button" class="layui-btn layui-btn-primary ajax-photos"><i class="fa fa-file-image-o"></i> 选择图片</button>
                    EOT;
                }
                
                break;
            //日期
            case "date":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data[$field['name']]}" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }else if($data and isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data['jdata'][$field['name']]}" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }
                
                break;
            //日期区间
            case "date_range":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data[$field['name']]}" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }else if($data and isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data['jdata'][$field['name']]}" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="" id="d_{$field['name']}" autocomplete="off" placeholder="" class="layui-input"  {$required}>
                    EOT;
                }
                
                break;
            //省市联动
            case "shengshi":
                if($data){
                    $ret = <<<EOT
                        <input id="shengshi_{$field['name']}" name="{$field['name']}" value="{$data['province']},{$data['city']},{$data['area']}" placeholder="请选择" class="layui-hide"  {$required}/>
                        <input name="province" type="hidden"><input name="city" type="hidden"><input name="area" type="hidden">
                    EOT;
                }else{
                    $ret = <<<EOT
                        <input id="shengshi_{$field['name']}" name="{$field['name']}" value="" placeholder="请选择" class="layui-hide"  {$required}/>
                        <input name="province" type="hidden"><input name="city" type="hidden"><input name="area" type="hidden">
                    EOT;
                }
                
                break;
            //绑定字典
            case "dict":
                $ret = '<option value=""></option>';
                foreach (get_dict_list($field['value']) as $k=>$v) {
                    if(isset($data[$field['name']]) and $k == $data[$field['name']] ){
                        $ret .= "<option value=\"$k\" selected>$v</option>";
                    }elseif(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret .= "<option value=\"$k\" selected>$v</option>";
                    }else{
                        $ret .= "<option value=\"$k\">$v</option>";
                    }
                }
                $ret = "<select name=\"{$field['name']}\" lay-search  {$required}>" . $ret . '</select>';
                break;
            default:
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data[$field['name']]}" autocomplete="off" placeholder="" class="layui-input" {$required}>
                    EOT;
                }elseif(isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="{$data['jdata'][$field['name']]}" autocomplete="off" placeholder="" class="layui-input" {$required}>
                    EOT;
                }else{
                    $ret = <<<EOT
                        <input type="text" name="{$field['name']}" value="" autocomplete="off" placeholder="" class="layui-input" {$required}>
                    EOT;
                }
                
                break;
        }
        return $ret;
    }
    //获取字段代码
    static function get_field_code_js($data) {
        $ret = '';
        switch ($data['type']){
            //编辑器
            case "editor":
                $ret = <<<EOT
                    <script src="/static/libs/ueditor/ueditor.config.js"></script>
                    <script src="/static/libs/ueditor/ueditor.all.min.js"></script>
                    <script src="/static/libs/ueditor/lang/zh-cn/zh-cn.js"></script>
                    <script>
                        UE.getEditor('editor_{$data['name']}',{
                            // initialFrameWidth :800,// 设置编辑器宽度
                            initialFrameHeight:400,// 设置编辑器高度
                            scaleEnabled:true,
                            serverUrl: "{:url('admin/uploads/editor')}"
                        });
                    </script>
                EOT;
                break;
            //日期
            case "date":
                $ret = <<<EOT
                    <script type="text/javascript">
                        laydate.render({
                            elem: '#d_{$data['name']}'
                        });
                    </script>
                EOT;
                break;
            //日期区间
            case "date_range":
                $ret = <<<EOT
                    <script type="text/javascript">
                        laydate.render({
                            elem: '#d_{$data['name']}'
                            ,range: true
                        });
                    </script>
                EOT;
                break;
            //日期区间
            case "shengshi":
                $ret = <<<EOT
                    <script type="text/javascript" src="/static/hqui/module/cascader/citys-data.js"></script>
                    <script>
                        layui.use(['cascader'], function () {
                            var $ = layui.jquery;
                            var cascader = layui.cascader;
                            var admin = layui.admin;
                            var data = cascader.getCityData(admin.util.deepClone(citysData));
                            cascader.render({
                                elem: '#shengshi_{$data['name']}',
                                data: data
                            });

                        });
                    </script>
                EOT;
                break;
            default:
                $ret = '';
                break;
        }
        return $ret;
    }
    //获取字段代码
    static function get_field_code_value($field,$data) {
        $ret = '';
        switch ($field['type']){
            //多行输入
            case "textarea":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = $data[$field['name']];
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = $data['jdata'][$field['name']];
                }else{
                    $ret = "";
                }
                break;
            //下拉选项
            case "select":
                foreach (parse_attr($field['options']) as $k=>$v) {
                    if(isset($data[$field['name']]) and $k == $data[$field['name']] and $field['is_sys'] == '1'){
                        $ret = $v;
                    }else if(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret = $v;
                    }else{
                        $rets = '';
                    }
                }
                break;
            //单选按钮
            case "radio":
                foreach (parse_attr($field['options']) as $k=>$v) {
                    if(isset($data[$field['name']]) and $k == $data[$field['name']] and $field['is_sys'] == '1'){
                        $ret = $v;
                    }else if(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret = $v;
                    }else{
                        $rets = '';
                    }
                }
                break;
            //图片上传
            case "upload":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = <<<EOT
                        <div class="paper-info-group-imgs" id="eDialogInfoImgs_{$field['name']}">
                            <img src="{$data[$field['name']]}" height="80px">
                        </div>
                    EOT;
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = <<<EOT
                        <div class="paper-info-group-imgs" id="eDialogInfoImgs_{$field['name']}">
                            <img src="{$data['jdata'][$field['name']]}" height="80px">
                        </div>
                    EOT;
                }else{
                    $ret = "";
                }
                
                break;
            //编辑器
            case "editor":
                if(isset($data['jdata'][$field['name']]) and $field['is_sys'] == '1'){
                    $ret = html_entity_decode($data[$field['name']]);
                }else if(isset($data['jdata'][$field['name']])){
                    $ret = html_entity_decode($data['jdata'][$field['name']]);
                }else{
                    $ret = "";
                }
                break;
            //多图上传
            case "photo":
                $ret = '';
                if(isset($data['photo']) and $field['is_sys'] == '1'){
                    $ret .= <<<EOT
                        <div class="paper-info-group-imgs" id="eDialogInfoImgs_{$field['name']}">
                    EOT;
                    foreach ($data['photo'] as $v) {
                        $ret .= '<img src="'.$v.'" height="80px">';
                    }
                    $ret .= <<<EOT
                        </div>
                    EOT;
                }else if(isset($data['jdata']['photo']) and $data['jdata']['photo'] != ''){
                    $ret .= <<<EOT
                        <div class="paper-info-group-imgs" id="eDialogInfoImgs_{$field['name']}">
                    EOT;
                    foreach ($data['jdata']['photo'] as $v) {
                        $ret .= '<img src="'.$v.'" height="80px">';
                    }
                    $ret .= <<<EOT
                        </div>
                    EOT;
                }else{
                    $ret = "";
                }
                
                break;
            //日期
            case "date":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = $data[$field['name']];
                }else if($data and isset($data['jdata'][$field['name']])){
                    $ret = $data['jdata'][$field['name']];
                }else{
                    $ret = "";
                }
                
                break;
            //日期区间
            case "date_range":
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = $data[$field['name']];
                }else if($data and isset($data['jdata'][$field['name']])){
                    $ret = $data['jdata'][$field['name']];
                }else{
                    $ret = "";
                }
                
                break;
            //省市联动
            case "shengshi":
                if($data){
                    $ret = $data['province'].','.$data['city'].','.$data['area'];
                }else{
                    $ret = "";
                }
                
                break;
            //绑定字典
            case "dict":
                foreach (get_dict_list($field['value']) as $k=>$v) {
                    if(isset($data[$field['name']]) and $k == $data[$field['name']] ){
                        $ret = $v;
                    }elseif(isset($data['jdata'][$field['name']]) and $k == $data['jdata'][$field['name']] ){
                        $ret = $v;
                    }else{
                        $rets = '';
                    }
                }
                break;
            default:
                if(isset($data[$field['name']]) and $field['is_sys'] == '1'){
                    $ret = $data[$field['name']];
                }elseif(isset($data['jdata'][$field['name']])){
                    $ret = $data['jdata'][$field['name']];
                }else{
                    $ret = "";
                }
                
                break;
        }
        return $ret;
    }
    //获取字段代码
    static function get_field_code_value_js($data) {
        $ret = '';
        switch ($data['type']){
            //编辑器
            case "upload":
                $ret = <<<EOT
                    <script>
                        layer.photos({
                            photos: '#eDialogInfoImgs_{$data['name']}',
                            shade: .1,
                            closeBtn: true
                        });
                    </script>
                EOT;
                break;
            //日期
            case "photo":
                $ret = <<<EOT
                    <script>
                        layer.photos({
                            photos: '#eDialogInfoImgs_{$data['name']}',
                            shade: .1,
                            closeBtn: true
                        });
                    </script>
                EOT;
                break;
            default:
                $ret = '';
                break;
        }
        return $ret;
    }
    
    
}